<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class winner extends admin
{

    private $db;

    public $site;
    public $award=array();


    function __construct()
    {
        parent::__construct();
        pc_base::load_app_func('global');
        $this->db = pc_base::load_model('contest_winner_model'); // 加载model
        $this->siteid = get_siteid(); // 获取当前站点
        $this->admin_username = param::get_cookie('admin_username'); // 获取当前登录的管理员
        $model_award= pc_base::load_model('contest_award_model'); // 加载model
        if ( isset($_REQUEST['contest_id']) && !empty($_REQUEST['contest_id'])) {
            $this->award= $model_award->getList($_REQUEST['contest_id']);
        }



    }

//列表
    public function init()
    {
        if(empty($this->award)){
            $is_activity = $_GET['is_activity'];
            if ($is_activity==0){
                showmessage(L('no_award_contest'), '?m=contest&c=award&a=init&id='.$_GET['contest_id'].'&contest_id='.$_GET['contest_id']);
            }else{
                showmessage(L('no_award_activity'), '?m=contest&c=award&a=init&id='.$_GET['contest_id'].'&contest_id='.$_GET['contest_id']);
            }
        }
        if (! isset($_REQUEST['contest_id']) || empty($_REQUEST['contest_id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $contest_id= $_REQUEST['contest_id'];
        $id= $_REQUEST['contest_id'];
        $model_contest = pc_base::load_model('contest_model'); // 加载model
        $data = $model_contest->get_one(array(
            'id' => $contest_id
        ));

        $where = "";
        $show_dialog = $show_validator = true;
        $show_header = '';
        $where .= "status = 1 and contest_id=".$contest_id." and siteid =" . $this->siteid;


        // 搜索框
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';


        if (isset($_GET['search'])) {
            if ($keyword) {
                    $where .= " and `name` like '%$keyword%'";
            }

        }

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $contestinfo = $this->db->listinfo($where, 'id DESC', $page, 10);
        $admin_model = pc_base::load_model('admin_model'); // 加载admin_model
        $administrators = $admin_model->getRealnameInfo();
        foreach ($contestinfo as $key=>$value){
            foreach ($administrators as $key2=>$value2){
                if($value['updated_by'] == $key2){
                    $value['publisher'] = $value2;
                    $contestinfo[$key] = $value;
                    break;
                }
            }
        }
        $pages = $this->db->pages;

        include $this->admin_tpl('contest_winner_list');
    }

    /**
     * 添加
     */
    public function add()
    {


        if (isset($_POST['dosubmit'])) {

            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $data['created_by'] =  $_SESSION['userid'];
            $data['updated_by'] =  $_SESSION['userid'];

            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] =date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $result = $this->db->insert($data);
            if ($result) {
                showmessage(L('add_success'), '?m=contest&c=winner&a=init&contest_id='.$data['contest_id'],'','add');
            } else {
                showmessage(L('add_error'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            if (! isset($_REQUEST['contest_id']) || empty($_REQUEST['contest_id'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $contest_id= $_REQUEST['contest_id'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            //moto
            $talent = pc_base::load_model('contest_talent_model'); // 加载model
            $moto=$talent->moto_list($contest_id);


            include $this->admin_tpl('contest_winner_add');
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
        $data = $this->db->get_one(array(
            'id' => $id
        ));
        $model_award= pc_base::load_model('contest_award_model'); // 加载model
        $this->award= $model_award->getList($data['contest_id']);

        $upinfo = $_POST['data'];
        if (isset($_POST['dosubmit'])) {

            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $data['updated_by'] =  $_SESSION['userid'];

            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $result = $this->db->update($data, array(
                'id' => $id
            ));
            if ($result) {
                showmessage(L('edit_success'), '?m=contest&c=winner&a=init&id='.$data['contest_id'],'','edit');
            } else {
                showmessage(L('edit_error'), HTTP_REFERER);
            }
        } else {
            //$show_dialog = $show_validator = true;
            $show_dialog = $show_validator = true;
            $show_header = '';
            //moto
            $talent = pc_base::load_model('contest_talent_model'); // 加载model
            $moto=$talent->moto_list($data['contest_id']);
            include $this->admin_tpl('contest_winner_edit');
        }
    }

    /**
     * 删除、批量删除
     */
    public function del()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $this->db->update(array(
            'status' => '2'
        ), "id in ('$id')");
        if ($result) {
            showmessage(L('del_success'), HTTP_REFERER);
        } else {
            showmessage(L('del_error'), HTTP_REFERER);
        }
    }


}